﻿
namespace SPL.Windows.Forms
{
	/// <summary>
	/// abstract Role typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class RoleDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<RoleDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Role _currentRole = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxRoleDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxRoleErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxRoleBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the Id property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Name property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNameDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Description property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDescriptionDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Value property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxValueDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Role> _RoleList;
				
		/// <summary> 
		/// The list of Role to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Role> RoleList
		{
			get {return this._RoleList;}
			set
			{
				this._RoleList = value;
				this.uxRoleBindingSource.DataSource = null;
				this.uxRoleBindingSource.DataSource = value;
				this.uxRoleDataGridView.DataSource = null;
				this.uxRoleDataGridView.DataSource = this.uxRoleBindingSource;				
				//this.uxRoleBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxRoleBindingSource_ListChanged);
				this.uxRoleBindingSource.CurrentItemChanged += new System.EventHandler(OnRoleBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnRoleBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentRole = uxRoleBindingSource.Current as Entities.Role;
			
			if (_currentRole != null)
			{
				_currentRole.Validate();
			}
			//_Role.Validate();
			OnCurrentEntityChanged();
		}

		//void uxRoleBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Role"/> instance.
		/// </summary>
		public Entities.Role SelectedRole
		{
			get {return this._currentRole;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxRoleDataGridView.VirtualMode;}
			set
			{
				this.uxRoleDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxRoleDataGridView.AllowUserToAddRows;}
			set {this.uxRoleDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxRoleDataGridView.AllowUserToDeleteRows;}
			set {this.uxRoleDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxRoleDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxRoleDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="RoleDataGridViewBase"/> class.
		/// </summary>
		public RoleDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxRoleDataGridView = new System.Windows.Forms.DataGridView();
			this.uxRoleBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxRoleErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNameDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDescriptionDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxValueDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxRoleDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRoleBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRoleErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxRoleErrorProvider
			// 
			this.uxRoleErrorProvider.ContainerControl = this;
			this.uxRoleErrorProvider.DataSource = this.uxRoleBindingSource;						
			// 
			// uxRoleDataGridView
			// 
			this.uxRoleDataGridView.AutoGenerateColumns = false;
			this.uxRoleDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxRoleDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdDataGridViewColumn,
		this.uxNameDataGridViewColumn,
		this.uxDescriptionDataGridViewColumn,
		this.uxValueDataGridViewColumn			});
			this.uxRoleDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxRoleDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxRoleDataGridView.Name = "uxRoleDataGridView";
			this.uxRoleDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxRoleDataGridView.TabIndex = 0;	
			this.uxRoleDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxRoleDataGridView.EnableHeadersVisualStyles = false;
			this.uxRoleDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnRoleDataGridViewDataError);
			this.uxRoleDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRoleDataGridViewCellValueNeeded);
			this.uxRoleDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRoleDataGridViewCellValuePushed);
			
			//
			// uxIdDataGridViewColumn
			//
			this.uxIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdDataGridViewColumn.DataPropertyName = "Id";
			this.uxIdDataGridViewColumn.HeaderText = "Id";
			this.uxIdDataGridViewColumn.Name = "uxIdDataGridViewColumn";
			this.uxIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxNameDataGridViewColumn
			//
			this.uxNameDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNameDataGridViewColumn.DataPropertyName = "Name";
			this.uxNameDataGridViewColumn.HeaderText = "Name";
			this.uxNameDataGridViewColumn.Name = "uxNameDataGridViewColumn";
			this.uxNameDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNameDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNameDataGridViewColumn.ReadOnly = false;		
			//
			// uxDescriptionDataGridViewColumn
			//
			this.uxDescriptionDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDescriptionDataGridViewColumn.DataPropertyName = "Description";
			this.uxDescriptionDataGridViewColumn.HeaderText = "Description";
			this.uxDescriptionDataGridViewColumn.Name = "uxDescriptionDataGridViewColumn";
			this.uxDescriptionDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDescriptionDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDescriptionDataGridViewColumn.ReadOnly = false;		
			//
			// uxValueDataGridViewColumn
			//
			this.uxValueDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxValueDataGridViewColumn.DataPropertyName = "Value";
			this.uxValueDataGridViewColumn.HeaderText = "Value";
			this.uxValueDataGridViewColumn.Name = "uxValueDataGridViewColumn";
			this.uxValueDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxValueDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxValueDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxRoleDataGridView);
			this.Name = "RoleDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxRoleErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRoleDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRoleBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				RoleDataGridViewEventArgs args = new RoleDataGridViewEventArgs();
				args.Role = _currentRole;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class RoleDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Role	_Role;
	
			/// <summary>
			/// the  Entities.Role instance.
			/// </summary>
			public Entities.Role Role
			{
				get { return _Role; }
				set { _Role = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxRoleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnRoleDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxRoleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRoleDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRoleDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						e.Value = RoleList[e.RowIndex].Id;
						break;
					case "uxNameDataGridViewColumn":
						e.Value = RoleList[e.RowIndex].Name;
						break;
					case "uxDescriptionDataGridViewColumn":
						e.Value = RoleList[e.RowIndex].Description;
						break;
					case "uxValueDataGridViewColumn":
						e.Value = RoleList[e.RowIndex].Value;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxRoleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRoleDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRoleDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						RoleList[e.RowIndex].Id = (System.Int32)e.Value;
						break;
					case "uxNameDataGridViewColumn":
						RoleList[e.RowIndex].Name = (System.String)e.Value;
						break;
					case "uxDescriptionDataGridViewColumn":
						RoleList[e.RowIndex].Description = (System.String)e.Value;
						break;
					case "uxValueDataGridViewColumn":
						RoleList[e.RowIndex].Value = (System.Int32?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
